from libs.exception import BaseHTTPException
from flask_babel import gettext as _


class EmailCodeLoginRateLimitExceededError(BaseHTTPException):
    error_code = "email_code_login_rate_limit_exceeded"
    description = "Too many login emails have been sent. Please try again in 5 minutes."
    code = 429
    def __init__(self,):
        self.description = _(EmailCodeLoginRateLimitExceededError.description)
        super().__init__(description=self.description)


class EmailCodeAccountDeletionRateLimitExceededError(BaseHTTPException):
    error_code = "email_code_account_deletion_rate_limit_exceeded"
    description = "Too many account deletion emails have been sent. Please try again in 5 minutes."
    code = 429
    def __init__(self,):
        self.description = _("Too many account deletion emails have been sent. Please try again in 5 minutes.")
        super().__init__(description=self.description)
    

class NotInitValidateError(BaseHTTPException):
    error_code = "not_init_validated"
    description = "Init validation has not been completed yet. Please proceed with the init validation process first."
    code = 401
    def __init__(self,):
        self.description = _("Init validation has not been completed yet. Please proceed with the init validation process first.")
        super().__init__(description=self.description)


class InitValidateFailedError(BaseHTTPException):
    error_code = "init_validate_failed"
    description = "Init validation failed. Please check the password and try again."
    code = 401
    def __init__(self,):
        self.description = _("Init validation failed. Please check the password and try again.")
        super().__init__(description=self.description)


class AccountNotLinkTenantError(BaseHTTPException):
    error_code = "account_not_link_tenant"
    description = "Account not link tenant."
    code = 403
    def __init__(self,):
        self.description = _("Account not link tenant.")
        super().__init__(description=self.description)


class AlreadyActivateError(BaseHTTPException):
    error_code = "already_activate"
    description = "Auth Token is invalid or account already activated, please check again."
    code = 403
    def __init__(self,):
        self.description = _("Auth Token is invalid or account already activated, please check again.")
        super().__init__(description=self.description)


class NotAllowedCreateTenant(BaseHTTPException):
    error_code = "not_allowed_create_workspace"
    description = "Workspace not found, please contact system admin to invite you to join in a workspace."
    code = 400
    def __init__(self,):
        self.description = _("Workspace not found, please contact system admin to invite you to join in a workspace.")
        super().__init__(description=self.description)



class AccountBannedError(BaseHTTPException):
    error_code = "account_banned"
    description = "Account is banned."
    code = 400
    def __init__(self,):
        self.description = _("Account is banned.")
        super().__init__(description=self.description)



class AccountInFreezeError(BaseHTTPException):
    error_code = "account_in_freeze"
    code = 400
    description = (
        "This email account has been deleted within the past 30 days"
        "and is temporarily unavailable for new account registration."
    )
    def __init__(self,):
        self.description = _("This email account has been deleted within the past 30 days\nand is temporarily unavailable for new account registration.")
        super().__init__(description=self.description)

    


class EmailSendIpLimitError(BaseHTTPException):
    error_code = "email_send_ip_limit"
    description = "Too many emails have been sent from this IP address recently. Please try again later."
    code = 429
    def __init__(self,):
        self.description = _("Too many emails have been sent from this IP address recently. Please try again later.")
        super().__init__(description=self.description)

class HttpAccountNotFound(BaseHTTPException):
    error_code = "account_not_found"
    description = "Account not found."
    code = 400
    def __init__(self,account_id:str|None=None):
        if account_id:
            self.description = _("Account {account_id} not found.").format(account_id=account_id)
        else:
            self.description = _("Account not found.")
        super().__init__(description=self.description)

class FilenameNotExistsError(BaseHTTPException):
    code = 400
    description = "The specified filename does not exist."
    def __init__(self):
        self.description = _("The specified filename does not exist.")
        super().__init__(description=self.description)


class RemoteFileUploadError(BaseHTTPException):
    code = 400
    description = "Error uploading remote file."
    def __init__(self):
        self.description  = _("Error uploading remote file.")
        super().__init__(description=self.description)

class HTTPNotLoginError(BaseHTTPException):
    code = 401
    description = "Please login first."
    def __init__(self):
        self.description  = _("Please login first.")
        super().__init__(description=self.description)

class ApiKeyAuthFailedError(BaseHTTPException):
    error_code = "auth_failed"
    description = "{message}"
    code = 500


class InvalidEmailError(BaseHTTPException):
    error_code = "invalid_email"
    description = "The email address is not valid."
    code = 400
    def __init__(self):
        self.description = _("The email address is not valid.")
        super().__init__(description=self.description)



class PasswordMismatchError(BaseHTTPException):
    error_code = "password_mismatch"
    description = "The passwords do not match."
    code = 400
    def __init__(self):
        self.description = _("The passwords do not match.")
        super().__init__(description=self.description)


class InvalidTokenError(BaseHTTPException):
    error_code = "invalid_or_expired_token"
    description = "The token is invalid or has expired."
    code = 400
    def __init__(self):
        self.description = _("The token is invalid or has expired.")
        super().__init__(description=self.description)


class PasswordResetRateLimitExceededError(BaseHTTPException):
    error_code = "password_reset_rate_limit_exceeded"
    description = "Too many password reset emails have been sent. Please try again in 1 minutes."
    code = 429
    def __init__(self):
        self.description = _("Too many password reset emails have been sent. Please try again in 1 minutes.")
        super().__init__(description=self.description)


class EmailCodeError(BaseHTTPException):
    error_code = "email_code_error"
    description = "Email code is invalid or expired."
    code = 400
    def __init__(self,):
        self.description = _("Email code is invalid or expired.")
        super().__init__(description=self.description)

class EmailPasswordResetLimitError(BaseHTTPException):
    error_code = "email_password_reset_limit"
    description = "Too many failed password reset attempts. Please try again in 24 hours."
    code = 429
    def __init__(self,):
        self.description = _("Too many failed password reset attempts. Please try again in 24 hours.")
        super().__init__(description=self.description)


class EmailOrPasswordMismatchError(BaseHTTPException):
    error_code = "email_or_password_mismatch"
    description = "The email or password is mismatched."
    code = 400
    def __init__(self,):
        self.description = _("The email or password is mismatched.")
        super().__init__(description=self.description)


class EmailPasswordLoginLimitError(BaseHTTPException):
    error_code = "email_code_login_limit"
    description = "Too many incorrect password attempts. Please try again later."
    code = 429
    def __init__(self,):
        self.description = _("Too many incorrect password attempts. Please try again later.")
        super().__init__(description=self.description)

class UnauthorizedAndForceLogout(BaseHTTPException):
    error_code = "unauthorized_and_force_logout"
    description = "Unauthorized and force logout."
    code = 401
    def __init__(self,message):
        self.description = _("Unauthorized and force logout.:{message}").format(message=message)
        super().__init__(description=self.description)

class WeChatLoginParameterError(BaseHTTPException):
    error_code = "wechat_login_parameter_error"
    description = "WeChat login parameter error.Tenant ID and WeChat App ID are required."
    code = 400
    def __init__(self):
        self.description = _("WeChat login parameter error.Tenant ID and WeChat App ID are required.")
        super().__init__(description=self.description)
